package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IGoldProductRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.community.service.IQuestionService;
import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.gold.domain.GoldProduct;
import cn.wolfcode.luowowo.gold.domain.GoldProductDetail;
import cn.wolfcode.luowowo.gold.service.IGoldProductDetailService;
import cn.wolfcode.luowowo.gold.service.IGoldProductService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 猫教授
 * @date 2019/8/17 17:18
 */
@Controller
public class DataController {
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IGoldProductService goldProductService;
    @Reference
    private IGoldProductDetailService goldProductDetailService;
    @Reference
    private IGoldProductRedisService goldProductRedisService;

    @Reference
    private IQuestionService questionService;

    @Reference
    private IQuestionSearchService questionSearchService;

    @Reference
    private IGoldProductSearchService goldProductSearchService;
    /**
     * 初始化用户数据到搜索引擎
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("dataIniti")
    public String dataIniti() {
        //初始化数据到es搜索引擎
        //先搬运所有的用户数据过来
        UserInfoTemplate tt = new UserInfoTemplate();
        List<UserInfo> userInfos = userInfoService.selectAll();
        for (UserInfo u : userInfos) {
            tt.setId(u.getId());
            tt.setDestName(u.getCity());
            tt.setFansnum(11);
            tt.setHeadUrl(u.getHeadImgUrl());
            tt.setInfo(u.getInfo());
            tt.setLevel(u.getLevel());
            tt.setNickname(u.getNickname());
            tt.setReplynum(10);
            tt.setTravelnum(2);
            userInfoTemplateSearchService.save(tt);
        }
        //先把那个游记数据搬运过来
        List<StrategyDetail> detailList = strategyDetailService.list();
        StrategyTemplate t = new StrategyTemplate();
        for (StrategyDetail detail : detailList) {
            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());
            t.setCoverUrl(detail.getCoverUrl());
//            String tags = strategyTagService.getTags(detail.getId());
//            List<String> list = Arrays.asList(tags.split(","));
//            t.setTags(list);
//
            //这里根据业务需求分许需要做省份和国家,以及国外的游记分组查询统计,所以我们需要在对象里面设置身份以及国家字段
            //这里是查询国家
            Destination dest = destinationService.getCountry(detail.getDest().getId());
            t.setCountryId(dest.getId());
            t.setCountryName(dest.getName());
            //这里查询省份数据
            dest = destinationService.getProvince(detail.getDest().getId());
            if (dest != null) {
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }
            strategyTemplateSearchService.save(t);
        }
        //同步地点
        //先查询出所有的地点
        List<Destination> ds = destinationService.selectAll();
        DestinationTemplate ttt = new DestinationTemplate();
        for (Destination d : ds) {
            ttt.setId(d.getId());
            ttt.setCoverUrl(d.getCoverUrl());
            ttt.setName(d.getName());
            ttt.setInfo(d.getInfo());
            destinationSearchService.saveOrUpdate(ttt);
        }
        //同步游记
        //先查出所有的游记
        List<Travel> ts=travelService.selectAll();
        TravelTemplate tr = new TravelTemplate();
        for (Travel ttr : ts) {
            tr.setId(ttr.getId());
            tr.setAuthorId(ttr.getAuthor().getId());
            tr.setAuthorName(ttr.getAuthor().getNickname());
            tr.setCoverUrl(ttr.getCoverUrl());
            tr.setCreateTime(ttr.getCreateTime());
            tr.setDestId(ttr.getDest().getId());
            tr.setDestName(ttr.getDest().getName());
            tr.setReplynum(ttr.getReplynum());
            tr.setSummary(ttr.getSummary());
            tr.setTitle(ttr.getTitle());
            tr.setViewnum(ttr.getViewnum());
            travelSearchService.save(tr);
        }
        return "ok";
    }


    @ResponseBody
    @RequestMapping("goldIniti")
    public String goldIniti() {
        //首先,先查询出全部商品
        List<GoldProduct> products=goldProductService.selectAll();
        //遍历
        GoldProductTemplate goldProductTemplate = new GoldProductTemplate();
        for (GoldProduct product : products) {
            goldProductTemplate.setId(product.getId());
            goldProductTemplate.setConvertGoldNum(product.getConvertGoldNum());
            goldProductTemplate.setParentTypeId(product.getProductTypeParent().getId());
            goldProductTemplate.setParentTypeName(product.getProductTypeParent().getName());
            goldProductTemplate.setProductName(product.getProductName());
            goldProductTemplate.setSubTitle(product.getSubTitle());
            goldProductTemplate.setTypeId(product.getProductType().getId());
            goldProductTemplate.setTypeName(product.getProductType().getName());
            goldProductTemplate.setActualPrice(product.getActualPrice().intValue());
            goldProductTemplate.setSalesNum(product.getSalesNum());
            goldProductTemplate.setCoverUrl(product.getOneCoverUrl());
            goldProductTemplate.setOriginalPrice(product.getOriginalPrice().intValue());
            //然后进行保存
            goldProductSearchService.save(goldProductTemplate);
        }



        return "ok";
    }

    @ResponseBody
    @RequestMapping("questionCopy")
    public String questionCopy() {
        //首先,先查询出全部商品
        List<Question> questions=questionService.selectAll();
        //遍历
        QuestionTemplate questionTemplate = new QuestionTemplate();
        for (Question q : questions) {
            questionTemplate.setId(q.getId());
            questionTemplate.setCreateTime(q.getCreatetime());
            questionTemplate.setDestId(q.getDest().getId());
            questionTemplate.setDestName(q.getDest().getName());
            questionTemplate.setTitle(q.getTitle());
            questionTemplate.setContent(q.getContent());
            questionTemplate.setAuthorId(q.getUserInfo().getId());
            questionTemplate.setAuthorName(q.getUserInfo().getNickname());
            questionTemplate.setAuthorHeadImgUrl(q.getUserInfo().getHeadImgUrl());
            questionTemplate.setAuthorlevel(q.getUserInfo().getLevel());
            //然后进行保存
            questionSearchService.save(questionTemplate);
        }

        return "ok";
    }

    @ResponseBody
    @RequestMapping("detailIniti")
    public String detailIniti() {
        ////首先先准备数据
        List<GoldProduct> products=goldProductService.selectAll();
        for (GoldProduct product : products) {
            if (product.getId()%3==0){
                GoldProductDetail detail = new GoldProductDetail();

                detail.setProductId(product.getId());
                List<String> str=new ArrayList<>();
                for(int i=101;i<=105;i++){
                    str.add("/"+i+".jpg");
                }
                detail.setDetail(str);
                goldProductDetailService.save(detail);
            }else if (product.getId()%2==0){
                GoldProductDetail detail = new GoldProductDetail();

                detail.setProductId(product.getId());
                List<String> str=new ArrayList<>();
                for(int i=111;i<=115;i++){
                    str.add("/"+i+".jpg");
                }
                detail.setDetail(str);
                goldProductDetailService.save(detail);
            }else {
                GoldProductDetail detail = new GoldProductDetail();

                detail.setProductId(product.getId());
                List<String> str=new ArrayList<>();
                for(int i=121;i<=125;i++){
                    str.add("/"+i+".jpg");
                }
                detail.setDetail(str);
                goldProductDetailService.save(detail);
            }
        }
        return "ok";
    }
    @ResponseBody
    @RequestMapping("saleIniti")
    public String saleIniti() {
        ////首先先准备数据
        List<GoldProduct> products=goldProductService.selectAll();
        for (GoldProduct product : products) {
            //对排行榜进行操作
            goldProductRedisService.addSale(product.getProductType().getId(),product.getId(),product.getSalesNum());
        }
        return "ok";
    }
    @ResponseBody
    @RequestMapping("storeIniti")
    public String storeIniti() {
        ////首先先准备数据
        List<GoldProduct> products=goldProductService.selectAll();
        for (GoldProduct product : products) {
            //对排行榜进行操作
            goldProductRedisService.addStoreSale(RedisKeys.STORE_HOT_SALE.getPrefix(),product.getId().toString(),product.getSalesNum());
        }
        return "ok";
    }



}
